<template>
  <div style="background-color: #f5f6f7; width: 958px">
    <div class="news-edit-news-frame" >
      <el-form :model="editForm" :rules="rules" ref="editForm"  class="news-edit-news-inputForm">
        <el-form-item label="标题" prop="title" class="el-form-item">
          <el-input v-model="editForm.title" placeholder="输入标题" maxlength="25" clearable></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="subType" class="el-form-item-right">
          <el-select class="news-edit-news-selector" v-model="editForm.subType" clearable filterable placeholder="选择类别">
            <el-option v-for="item in subTypeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime" class="el-form-item">
          <!-- 强制换行 -->
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <el-date-picker type="datetime" size="medium" align="left" v-model="editForm.publishTime" clearable
                            format="yyyy-MM-dd HH:mm" value-format="yyyy-MM-dd HH:mm"  placeholder="选择发布时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div style="width:958px;border-bottom: 1px solid #e5e5e5;height: 30px;margin-top: 5px;">
        <span style="margin-left: 20px;"><span class="news-edit-news-red-dot">*&nbsp;</span>内容</span>
      </div>
      <div>
        <Uediter :value="editForm.content" :config="ueditor.config" ref="ue"></Uediter>
      </div>
    </div>
    <el-row class="news-edit-news-btnFrame">
      <el-button class="news-edit-news-btn" @click="submitForm('editForm')" type="primary">{{btnTitle}}</el-button>
    </el-row>
  </div>
</template>
<script>
  import maValidate from 'utils/common-method/validate/index';
  import {validateTips} from 'utils/tips';
  import Uediter from '@/components/ueditor/UE.vue';
  export default {
    components: {
      Uediter
    },
    props: ['newsInfo', 'btnTitle'],
    data() {
      return {
        initForm: JSON.stringify(this.newsInfo),
        editForm: JSON.parse(JSON.stringify(this.newsInfo)),
        rules: {
          title: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'},
            {min: 0, max: 25, message: validateTips.LESS_25, trigger: 'blur'}
          ],
          subType: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'}
          ],
          publishTime: [
            {required: true, message: validateTips.FILL_BASE_INFO, trigger: 'blur'}
          ]
        },
        subTypeOption: [
          {
            value: 11,
            label: '行情'
          }, {
            value: 12,
            label: '技术'
          }, {
            value: 13,
            label: '消费'
          }, {
            value: 14,
            label: '贸易'
          }, {
            value: 15,
            label: '专访'
          }, {
            value: 16,
            label: '投资并购'
          }, {
            value: 17,
            label: '公司'
          }, {
            value: 18,
            label: '政策'
          }
        ],
        ueditor: { // 富文本编辑
          value: '', // 默认文字
          config: {
            toolbars: [[// 工具条
              'fullscreen', 'source', 'undo', 'redo', 'bold', 'italic',
              'underline', 'fontborder', 'backcolor', 'fontsize', 'fontfamily',
              'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify',
              'strikethrough', 'superscript', 'subscript', 'removeformat',
              'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
              'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',
              'selectall', 'cleardoc', 'link', 'unlink', 'emotion', 'insertimage', 'map'
            ]],
            initialFrameWidth: 956,
            initialFrameHeight: 520,
            autoHeightEnabled: false,
            scaleEnabled: false, // 设置不自动调整高度
            elementPathEnabled: false, // 删除元素路径
            wordCount: false    // 删除字数统计
          }
        }
      };
    },
    created() {
      this.editForm = JSON.parse(JSON.stringify(this.newsInfo));
    },
    methods: {
      // 校验整数
      verifyInteger(val, prop) {
        maValidate.verifyInteger(val, prop);
      },
      // 校验小数
      verifyDecimal(val, prop) {
        maValidate.verifyDecimal(val, prop);
      },
      // 点击提交按钮
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.commitForm();
          } else {
            return false;
          }
        });
      },
      // 请求后端接口
      commitForm() {
        // 内容检查：新闻内容不能为空
        this.editForm.content = this.$refs.ue.getUEContent();
        if (this.editForm.content === null || this.editForm.content === '') {
          this.errorTip('内容不能为空，请补充');
          return;
        }
        // 内容检查：判断提交前后有无内容变化
        if (this.initForm === JSON.stringify(this.editForm)) {
          this.errorTip('内容没有任何变化，请确认');
          return;
        }
        var that = this;
        this.ajax({
          type: 'POST',
          url: '/malisk/server/news/saveNewsReport',
          data: {
            id: that.editForm.id,
            version: that.editForm.version,
            mainType: 1,
            subType: that.editForm.subType,
            title: that.editForm.title,
            content: that.editForm.content,
            publishTime: that.editForm.publishTime + ':00'
          },
          loading: true,
          success: (res) => {
            setTimeout(() => {
              this.$pcNProgress.done();
            }, 300);
            if (res.code === 200) {
              if (that.editForm.id === null || that.editForm.id === '') {
                this.successTip('发布成功', 2000);
              } else {
                this.successTip('保存完成', 2000);
              }
              that.$emit('sure');
            } else {
              this.errorTip(res.message);
            }
          }
        });
      }
    }
  };
</script>
<style>
  .news-edit-news-frame {
    display: flex;
    flex-wrap: wrap;
    width: 958px;
    height: 810px;
    border: 1px solid #e5e5e5;
    background-color: white
  }
  .news-edit-news-frame .el-form-item {
    display: inline-block;
    width: 420px;
    margin-left: 20px;
    margin-right: 20px;
  }
  .news-edit-news-frame .el-form-item-right {
    float: right;
  }
  .news-edit-news-inputForm {
    margin-top:10px;
  }
  .news-edit-news-btnFrame {
    width: 100%;
    margin-top: 40px;
    margin-bottom: 20px;
    text-align: center;
  }
  .news-edit-news-btn {
    width: 140px;
    height: 40px;
  }
  .news-edit-news-selector {
    width: 420px;
  }
  .news-edit-news-red-dot{
    color:#ff6c2b;
    font-size:21px;
    position:relative;
    top:8px;
  }
</style>
